<?php

if (empty($_POST["server"]))
	exit ("No server, morron!");
	
if ($_POST["port"] <= 0)
	exit ("What port?!");

$ftpLink  = $_POST["server"];
$port 	  = $_POST["port"];
$user 	  = $_POST["login"];
$password = $_POST["pass"];
$path	  = $_POST["directory"];

$dir = '../';

if (!isset($_FILES['upfile']))
	exit ("File is not set");

$upFile 	= $_FILES['upfile']['tmp_name'];
$upFileName = $_FILES['upfile']['name'];
$upFileSize = $_FILES['upfile']['size'];
$upFileType = $_FILES['upfile']['type'];
$errorCode  = $_FILES['upfile']['error'];

if ($errorCode == 0)
{
	$upFileName = $dir . $upFileName;
	copy($upFile, $upFileName);
}   

//upload file from server
$tmp   = $_FILES['upfile']['tmp_name'];
$aname = $_FILES['upfile']['name'];


//CONNECTION-----------------------------------------------------------------------
$connect = ftp_connect($ftpLink, $port, 10);
if (empty($connect)) 
{
	unlink($upFileName);
	exit ("Can not connect to '$ftpLink'");
}

if (!ftp_login($connect, $user, $password))
{
	ftp_close($connect);
	unlink($upFileName);
	exit ("Can not login as '$user'. Ensure your UserName and Password.");
}
//passive mode on
ftp_pasv($connect, true);

//CONNECTION-----------------------------------------------------------------------
	

if (!ftp_chdir($connect, $path))
{	
	ftp_close($connect);
	unlink($upFileName);
	exit ("Can not find directory '$path'");
}

if (!ftp_put($connect, $aname, $upFileName, FTP_BINARY))
{
	ftp_close($connect);
	unlink($upFileName);
	exit ("Can not upload file.");
}
	
ftp_close($connect);
unlink($upFileName);

echo "File was successfully uploaded!<br/>";
echo "<form action='chdir.php' method='post'>
		<input type='hidden' name='server' value='$ftpLink'>
		<input type='hidden' name='port' value='$port'>
		<input type='hidden' name='login' value='$user'>
		<input type='hidden' name='pass' value='$password'>
		<input type='hidden' name='folder' value='$path'>
		<input type='submit' value='Back'>";

?>